package com.base.interceptor;

/**
 * \* @author: hasee
 * \* Date: 2024/6/9
 * \* Time: 22:59
 * \* Description:
 * \
 */
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 检查用户是否已经登录
        if ("/base/handle/login".equals(request.getRequestURI())||"/base".equals(request.getRequestURI()) ||"/base/handle/admin/login".equals(request.getRequestURI()) || request.getSession().getAttribute("user") != null) {
            return true; // 允许访问
        }
        // 用户未登录，跳转到登录页面
        response.sendRedirect("/base/login");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        // 可以在这里对modelAndView进行操作
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 请求处理完成后进行操作
    }
}